import { getOEMConfig } from '@/config';
interface FileSrcOption {
  width?: number;
  height?: number;
  format?: 'webp' | 'png' | 'jpg';
  /**
   * 如果 hash 是 hash[], 则split 后取 index
   */
  index?: number;
  /** 是否返回源文件 */
  source?: boolean;
  https?: boolean;
}

export const $src = (
  hash?: string,
  { width = 1000, height, format, index, source, https = true }: FileSrcOption = {},
): string => {
  if (!hash) return '';
  if (typeof hash === 'string' && hash.slice(0, 4) === 'http') return hash;
  let zip = source
    ? ''
    : `?imageView2/0${width ? '/w/' + width : ''}${height ? '/h/' + height : ''}${
        format ? '/format/' + format : ''
      }`;
  if (typeof index === 'string') {
    let hashs = hash.split(',');
    return hashs[index]
      ? `http${https ? 's' : ''}://${getOEMConfig().cdnDomain}/` + hashs[index] + zip
      : '';
  } else {
    return `http${https ? 's' : ''}://${getOEMConfig().cdnDomain}/` + hash + zip;
  }
};
